import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

import data.Database;
import data.Element;
import data.Option;


public class RequestThread extends Thread {
	Socket socket;
	Scanner netIn;
	PrintWriter netOut;
	
	Database db;
	
	public RequestThread( Socket s, Database db ) {
		socket = s;
		this.db = db;
	}

	public void run() {
		try {
			netIn = new Scanner(socket.getInputStream());
			netOut = new PrintWriter(socket.getOutputStream());
		} catch (Exception e) {
			System.out.println("Could not make net IO objects.");
		}
		System.out.println("Created net IO objects.");

		while (socket != null) {
			String input = null;
			try{
				input = netIn.nextLine().trim();
			} catch (NoSuchElementException e) {
				e.printStackTrace();
			}
			if (input != null) {
				System.out.println("Recieved input: " + input);
				String command = null;
				String query = null;
				String options = null;
				if (input.contains(":")) {
					command = input.substring(0, input.indexOf(":"));
					query = input.substring(input.indexOf(":")+1);
					options = input.substring(input.indexOf("$")+1, input.length());

					ArrayList<Option> optionList = new ArrayList<Option>();
					
					while (options.indexOf("$") != -1) {
						String option = options.substring(0,options.indexOf("$"));
						System.out.println("NEW OPTION"+option);
						optionList.add(new Option(option));
						options = options.substring(options.indexOf("$")+1);
					}
					
					if (command.equals("search")) {
						System.out.println("Preparing to search!");
						
						String results = null;
						synchronized(db) {
							results = db.search(query, optionList);
						}
						
						if (results == null) {
							sendMessage("none found;");
						}
						else 
							sendMessage(results + ";");
					}
				}
					else if (input.equals("getParams")) {
						System.out.println("Delivering possible search options to client.");
						sendMessage("params:nationality-NA,nationality-EU," +
								"year-2010,year-2011,year-2012" +
								"company-GE,company-Oracle," +
								"doctype-mri,doctype-discharge;");
					}
					else if (input.equals("bye") || input.equals("clientexit")) {
						System.out.println("Shutting down thread");
						socket = null;
					}
				}
				else {
					sendMessage("ECHO " + input + ";");
				}
			

			try {sleep(100);} catch(Exception e) {};
		}
	}	

	public boolean sendMessage( String s ) {
		boolean flag = false;
		while (!flag) {
			try {
				netOut.println(s);
				netOut.flush();
				flag = true;
			} catch (Exception e) {
				flag = false;
			}	
		}
		return true;
	}	

	public void close() {
		this.finalize();
	}
	
	public void finalize() {
		try {
			System.out.println("Destroying net stuff.");
			netOut.close();
			netIn.close();
			socket.close();
		} catch(Exception e) {}
		
		socket = null;
	}
}
